/*************************************************************************
> File Name: mystring.h
> Author: icebear
> Mail: 3322366195@qq.com
> Created Time: 2023-08-03 10:32:16
> Description: 重写字符串处理
************************************************************************/
#ifndef MYSTRING_H
#define MYSTRING_H

#include <stdio.h>
unsigned int strLength(char *str);
int strCopy(char *aim, char *src);
int strCompare(char *aim, char *src);
void strReverse(char *str);

unsigned int strLength(char *str)
{
    int length = 0;
    while (*(str + length) != '\0')
    {
        length++;
    }
    return length;
}

int strCopy(char *aim, char *src)
{
    char *str1 = aim, *str2 = src;
    while (*str2++ != '\0')
    {
        if (*str1++ = '\0')
        {
            return -1;
        }
    }
    str1 = aim, str2 = src;
    while (*str2 != '\0') // 加加不要放在 while里
    {
        *str1++ = *str2++;
    }
    *str1 = '\0';
}
int strCompare(char *aim, char *src)
{
    int flag = 0;
    char *str1 = aim, *str2 = src;
    while (*str1 != '\0' || *str2 != '\0')
    // 不要把++放在while里 短路造成死循环
    {

        if (*str1 == *str2)
        {
            str1++;
            str2++;
            continue;
        }
        if (*str1 < *str2)
        {
            flag = -1;
            break;
        }
        else
        {
            flag = 1;
            break;
        }
    }
    return flag;
}
void strReverse(char *str)
{
    char *p = str, *q = str;
    while (*q != '\0')
    {
        q++;
    }
    q -= 1;
    while (p < q) // 简化
    {
        printf("%c->%c\n", *p, *q);
        *p = *q ^ *p;
        *q = *q ^ *p;
        *p = *q ^ *p;
        p++;
        q--;
    }
}

// char *strCat(char *aim, char *str)
// {
// }
#endif